<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .list {
        width: 200px;
        height: 150px;
        background-color: skyblue;
        overflow: auto;
        margin: 0 auto;
      }

      .news {
        height: 30px;
      }

      /* 滚动条整体样式 */
      ::-webkit-scrollbar {
        /*高宽分别对应横竖滚动条的尺寸*/
        width: 15px;
        height: 15px;
      }

      /* // 滚动条里面小方块 */
      ::-webkit-scrollbar-thumb {
        border-radius: 10px;
        background-color: red;
      }

      /* // 滚动条里面轨道 */
      ::-webkit-scrollbar-track {
        background-color: yellow;
        border-radius: 10px;
      }
    </style>
  </head>

  <!-- 引入react核心库 -->
  <script src="../newJs/react.development.js"></script>
  <!-- 引入react-dom,用于支持react操作DOM -->
  <script src="../newJs/react-dom.development.js"></script>
  <!-- 引入babel,用于将jsx转换为js -->
  <script src="../newJs/babel.min.js"></script>

  <body>
    <!-- 容器 -->
    <div id="app"></div>
  </body>

  <script type="text/babel">
    class NewsList extends React.Component {
      state = { newsArr: [] };

      componentDidMount() {
        setInterval(() => {
          const { newsArr } = this.state;
          const newsItem = "新闻" + (newsArr.length + 1);
          this.setState({ newsArr: [newsItem, ...newsArr] });
        }, 1000);
      }

      getSnapshotBeforeUpdate() {
        return this.refs.list.scrollHeight;
      }

      componentDidUpdate(preProps, preState, height) {
        this.refs.list.scrollTop += this.refs.list.scrollHeight - height;
      }

      render() {
        return (
          <div className="list" ref="list">
            {this.state.newsArr.map((item, index) => {
              return (
                <div className="news" key={index}>
                  {item}
                </div>
              );
            })}
          </div>
        );
      }
    }

    ReactDOM.render(<NewsList />, document.getElementById("app"));
  </script>
</html>
